package com.jojo.config;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入拦截器");
        //判断haderller是否为控制层方法
        if (handler instanceof HandlerMethod){
            HandlerMethod handlerMethod= (HandlerMethod) handler;
            //获取方法名
            String name = handlerMethod.getMethod().getName();
            System.out.println("方法名"+name);
            Class<?> beanType = handlerMethod.getBeanType();
            System.out.println("控制层类名:"+beanType.getName());
        }

        //去session中获取登录标识
        HttpSession session = request.getSession();




        String username = (String) session.getAttribute("userLoginInfo");

        System.out.println(session.getAttribute("userLoginInfo")+"用户");
        //第一次登录没有session
        if(username!=null){
            return true;
        }else if (request.getRequestURI().contains("login")){
            return true;
        }

/*        User user=new User("admin","123");
//        User user = (User) session.getAttribute("userLoginInfo");
//        System.out.println(session.getAttribute("userLoginInfo"));
        if (user!=null){
            return true;
        }*/


        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
    }
}
